package com.atguigu.gmall.all.controller;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.item.client.ItemServiceClient;
import com.atguigu.gmall.model.product.BaseCategoryView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
public class IndexController {

    @Autowired
    private ItemServiceClient itemServiceClient;

    @Autowired//模板引擎（页面引擎）用来生成页面
    private TemplateEngine templateEngine;


    @GetMapping("/")
    public String index(Model model){

        model.addAttribute("list",getData());
        //首页
        return "index/index";
    }

    //使用静态化程序生成首页，保存在指定的盘符下，挂载到nginx中
    @GetMapping("/createHtml")
    @ResponseBody
    public Result createHtml(){
        //参数1 模板
        //参数2 数据//org.thymeleaf包下的
        Context context = new Context();
        context.setVariable("list",getData());//key value
        //参数3 输出流
        Writer writer= null;
        try {
            //字节流  ，字符流，编码集
           // writer = new OutputStreamWriter(new FileOutputStream("D:\excel\index.html"),"utf-8");
            //打印流
            writer=new PrintWriter("D:\\excel\\index.html","utf-8");
        } catch (IOException e) {
            e.printStackTrace();
        }
        templateEngine.process("index/index",context,writer);
        return Result.ok();
    }

    public List<Map> getData(){
        List<Map> listMap = new ArrayList<>();

        //所有分类
        List<BaseCategoryView> baseCategoryViewList =
                itemServiceClient.getBaseCategoryViewList();
        //根据一级分类ID 进行分组 返回值 Map
        Map<Long, List<BaseCategoryView>> baseCategoryViewListMap = baseCategoryViewList.
                stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));

        //1:前60个数据算一个Map
        baseCategoryViewListMap.forEach((k,v) -> {
            //一级分类
            Map category1Map = new HashMap();
            category1Map.put("index",k);
            category1Map.put("categoryId",k);
            category1Map.put("categoryName",v.get(0).getCategory1Name()); //V:  List<BaseCategoryView>

            //二级分类
            Map<Long, List<BaseCategoryView>> baseCategoryView2ListMap =
                    v.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));

            List<Map> listMap2 = new ArrayList<>();
            baseCategoryView2ListMap.forEach((k2,v2) -> {
                Map category2Map = new HashMap();
                category2Map.put("categoryId",k2);
                category2Map.put("categoryName",v2.get(0).getCategory2Name());
                //3:三级分类  V2: List<BaseCategoryView>
                List<Map> listMap3 = new ArrayList<>();
                v2.forEach(baseCategoryView -> {
                    Map category3Map = new HashMap();
                    category3Map.put("categoryId",baseCategoryView.getCategory3Id());
                    category3Map.put("categoryName",baseCategoryView.getCategory3Name());
                    listMap3.add(category3Map);
                });
                category2Map.put("categoryChild",listMap3);
                listMap2.add(category2Map);
            });
            category1Map.put("categoryChild",listMap2);
            listMap.add(category1Map);
        });
        return listMap;
    }

}
